package com.cg.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cg.Dto.InsertDictionDto;
import com.cg.Dto.SearchDictionaryDto;
import com.cg.Dto.UpdateDictinDto;
import com.cg.common.StringConstant;
import com.cg.entity.Dictionarydata;
import com.cg.entity.Personnel;
import com.cg.mapper.DictionarydataMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cg.result.PageVO;
import com.cg.result.R;
import com.cg.service.DictionarydataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhy
 * @since 2024-12-26
 */
@Service
public class DictionarydataServiceImpl extends ServiceImpl<DictionarydataMapper, Dictionarydata> implements DictionarydataService {
    @Override
    public R<?> search(SearchDictionaryDto searchDictionaryDto) {
     Page<Dictionarydata> pages =new Page<> (searchDictionaryDto.getPageNumber (),searchDictionaryDto.getPageSize ());
        LambdaQueryWrapper<Dictionarydata> wrapper = new LambdaQueryWrapper<Dictionarydata>()
                .like(StringUtils.isNotBlank(searchDictionaryDto.getValue ()),Dictionarydata::getValue,searchDictionaryDto.getValue ())
                .eq(searchDictionaryDto.getStatus() != null, Dictionarydata::getStatus,searchDictionaryDto.getStatus())
                .eq (Dictionarydata::getDictionaryListId,searchDictionaryDto.getDictionaryListId ());
     Page<Dictionarydata> page =this.page (pages,wrapper);
     PageVO<Dictionarydata> pageVO =new PageVO<> ();
     BeanUtils.copyProperties (page,pageVO);
     return R.Success (pageVO);
    }
    @Override
    public R<?> insert(InsertDictionDto insertDictionDto) {
        try {
            // 检查同一 dictionaryListId 下是否已经存在相同的 value
            LambdaQueryWrapper<Dictionarydata> wrapper = new LambdaQueryWrapper<Dictionarydata>()
                    .eq(Dictionarydata::getDictionaryListId, insertDictionDto.getDictionaryListId())
                    .eq(Dictionarydata::getValue, insertDictionDto.getValue());
            if (this.count(wrapper) > 0) {
                return R.Failed(StringConstant.VALUEISUSE);
            }
            // 创建新的 Dictionarydata 实体并复制属性
            Dictionarydata dictionarydata = new Dictionarydata();
            BeanUtils.copyProperties(insertDictionDto, dictionarydata);
            // 保存新的字典数据
            this.save(dictionarydata);
            return R.Success(StringConstant.INSERTSUCCESS);
        } catch (Exception e) {
            // 处理异常情况，并返回失败的结果
            e.printStackTrace(); // 或者记录日志
            return R.Failed(StringConstant.INSERTSUCCESS);
        }
    }
    @Override
    public R<?> updates(UpdateDictinDto updateDictinDto) {
        LambdaUpdateWrapper<Dictionarydata> wrapper =new LambdaUpdateWrapper<Dictionarydata> ()
                .set (Dictionarydata::getStatus,updateDictinDto.getStatus ())
                .eq ( Dictionarydata::getId,updateDictinDto.getId ());
        boolean update = this.update ( wrapper );
        if(update)
        {
            return  R.Success (StringConstant.UPDATESUCCESS);
        }
        return  R.Failed (StringConstant.UPDATEFAILED);
    }
}
